package cn.gok.server.controller;

import cn.gok.common.config.SystemLog;
import cn.gok.common.entity.customer.CustomerLevelInfo;
import cn.gok.common.resp.Result;
import cn.gok.server.service.CustomerLevelInfoService;
import cn.gok.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: DxlinY
 * @apiNote:
 * @date: 2020/11/11
 * @time: 14:55
 */
@RestController
@RequestMapping("/customerLevelInfo")
public class CustomerLevelInfoController {

    @Autowired
    private CustomerLevelInfoService customerLevelInfoService;

    /**
     * 用户等级信息维护分页
     *
     * @param page              当前页
     * @param limit             页容量
     * @param customerLevelInfo 搜索对象
     * @return result
     */
    @PostMapping("/page")
    @SystemLog(model = "用户等级信息", method = "用户等级信息维护")
    public Result page(int page, int limit, CustomerLevelInfo customerLevelInfo) {
        return ResultUtil.pageInfo(customerLevelInfoService.listAll(page, limit, customerLevelInfo));
    }


    /**
     * 通过id批量删除会员等级
     *
     * @param request
     * @return result
     */
    @PostMapping("/delByIds")
    @SystemLog(model = "用户等级信息", method = "批量删除")
    public Result delByIds(HttpServletRequest request) {
        String[] strIds = request.getParameterValues("ids");
        List <Integer> ids = new ArrayList <>();
        for (String id : strIds) {
            ids.add(Integer.parseInt(id));
        }
        if (customerLevelInfoService.delByIds(ids) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 修改
     *
     * @param customerLevelInfo 用户等级信息
     * @return result
     */
    @PostMapping("/updateById")
    @SystemLog(model = "用户等级信息", method = "修改")
    public Result updateById(CustomerLevelInfo customerLevelInfo) {
        if (customerLevelInfoService.updateById(customerLevelInfo) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 新增
     *
     * @param customerLevelInfo 用户等级信息
     * @return result
     */
    @PostMapping("/insert")
    @SystemLog(model = "用户等级信息", method = "新增")
    public Result insert(CustomerLevelInfo customerLevelInfo) {
        if (customerLevelInfoService.insertCustomerLevelInfo(customerLevelInfo) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 获取等级信息
     *
     * @param id 编号
     * @return result
     */
    @PostMapping("/getById")
    @SystemLog(model = "用户等级信息", method = "获取等级信息")
    public Result getById(Integer id) {
        return ResultUtil.success(customerLevelInfoService.getById(id));
    }
}
